﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Backup.Services.Logic;
using BackupObjects;

namespace Backup.Services.Entities
{
    /// <summary>
    /// Mapping BackupObject for communication.
    /// </summary>
    [DataContract]
    public class BackupEntity : ITaggableEntity
    {
        public BackupEntity()
        {
            Id = Guid.NewGuid();
            Visibility = VisibilityBackup.Private;
            TagIds = new List<Guid>();
        }

        internal BackupEntity(BackupObject backupObject)
            : this()
        {
            Id = backupObject.ID;
            Name = backupObject.Name;
            Visibility = backupObject.Visibility;
            OriginalFileName = backupObject.OriginalFileName;
            Description = backupObject.Description;
            UploadDate = backupObject.UploadDate;
            UploadBy = backupObject.UploadBy;
            if (backupObject.Contents != null)
            {
                Contents = (byte[])backupObject.Contents.Clone();
            }
            TagIds = backupObject.TagIds;
        }

        [DataMember]
        public Guid Id { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Description { get; set; }

        [DataMember]
        public byte[] Contents { get; set; }

        [DataMember]
        public string UploadBy { get; set; }

        [DataMember]
        public DateTime UploadDate { get; set; }

        [DataMember]
        public VisibilityBackup Visibility { get; set; }

        [DataMember]
        public string OriginalFileName { get; set; }

        public static implicit operator BackupEntity(BackupObject backupObject)
        {
            return backupObject == null ? null : new BackupEntity(backupObject);
        }

        public static implicit operator BackupObject(BackupEntity backupEntity)
        {
            return backupEntity == null
                       ? null
                       : new BackupObject()
                             {
                                 ID = backupEntity.Id,
                                 Name = backupEntity.Name,
                                 Description = backupEntity.Description,
                                 Contents = (byte[]) backupEntity.Contents.Clone(),
                                 OriginalFileName = backupEntity.OriginalFileName,
                                 UploadBy = backupEntity.UploadBy,
                                 UploadDate = backupEntity.UploadDate,
                                 Visibility = backupEntity.Visibility,
                                 TagIds = backupEntity.TagIds ?? new List<Guid>()
                             };
        }

        /// <summary>
        /// Gets or sets the tags for the curretn entity.
        /// </summary>
        public List<Guid> TagIds { get; set; }
    }
}
